package com.wyzt.item.service;

import com.wyzt.common.enums.ExceptionEnum;
import com.wyzt.common.exception.WyztException;
import com.wyzt.item.mapper.CategoryMapper;
import com.wyzt.item.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    public List<Category> queryCategoryListByPid(Long pid) {
        //查询条件，mapper会把对象中的非空属性作为查询条件
        Category t = new Category();
        t.setParentId(pid);
        List<Category> list = categoryMapper.select(t);
        //判断查询结果
        if (CollectionUtils.isEmpty(list)) {
            throw new WyztException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return list;
    }


    public List<Category> queryByBrandId(Long bid) {
        return categoryMapper.queryByBrandId(bid);
    }

    public List<Category> queryByIds(List<Long> ids) {
        List<Category> categories = categoryMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(categories)) {
            throw new WyztException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return categories;
    }

    public List<Category> queryParentByCid3(Long id) {

        //创建一个list集合来接收
        List<Category> categories = new ArrayList<>();
        Category category3 = categoryMapper.selectByPrimaryKey(id);
        Category category2 = categoryMapper.selectByPrimaryKey(category3.getParentId());
        Category category1 = categoryMapper.selectByPrimaryKey(category2.getParentId());
        categories.add(category1);
        categories.add(category2);
        categories.add(category3);
        return categories;
    }
}
